import { createRouter, createWebHistory } from "vue-router";
import AppLayout from '@/components/Layout/AppLayout.vue'
import Layout from '@/components/Layout/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/map",
      name: "可视化大屏",
      component: () => import("@/views/map.vue"),
      meta: { keepAlive: true, isAuth: true, needLogin: true },
    },
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/New/HomeNew/HomeNew.vue'),
      meta: {
        title: '农场总览',
        keepAlive: true,
        isAuth: true,
        needLogin: true
      }
    },
    {
      path: '/crops',
      name: 'crops',
      component: () => import('@/views/New/Crops/Crops.vue'),
      meta: {
        title: '作物监测',
        keepAlive: true,
        isAuth: true,
        needLogin: true
      }
    },
    {
      path: '/environment',
      name: 'environment',
      component: () => import('@/views/New/Environment/Environment.vue'),
      meta: {
        title: '环境监测',
        keepAlive: true,
        isAuth: true,
        needLogin: true
      }
    },
    {
      path: '/devices',
      name: 'devices',
      component: () => import('@/views/New/Devices/Devices.vue'),
      meta: {
        title: '智能设备',
        keepAlive: true,
        isAuth: true,
        needLogin: true
      }
    },
    {
      path: '/traceability',
      name: 'traceability',
      component: () => import('@/views/New/Traceability/Traceability.vue'),
      meta: {
        title: '产品溯源',
        keepAlive: true,
        isAuth: true,
        needLogin: true
      }
    },
    // {
    //   path: '/planting',
    //   name: 'planting',
    //   component: () => import('@/views/New/Planting/Planting.vue'),
    //   meta: {
    //     title: '种植计划',
    //     keepAlive: true,
    //     isAuth: true,
    //     needLogin: true
    //   }
    // },
    {
      path: '/digital-twin',
      name: 'digital-twin',
      component: () => import('@/views/map.vue'),
      meta: {
        title: '数字孪生',
        keepAlive: true,
        isAuth: true,
        needLogin: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import("@/components/Login/Login.vue"),
      meta: { title: '登录' }
    },
    {
      path: "/",
      component: () => import("@/components/Layout/index.vue"),
      meta: {needLogin: true},
      children: [
        {
          path: "devices",
          name: "设备管理",
          redirect: "/devices/list",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "list",
              name: "设备列表",
              component: () => import("@/views/Devices/DevicesList.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "status",
              name: "设备状态",
              component: () => import("@/views/Devices/DeviceStatus.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "settings",
              name: "设备设置",
              component: () => import("@/views/Devices/DevicesSettings.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "dashboard",
              name: "设备数据",
              component: () => import("@/views/Devices/DeviceDashboard.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },
        {
          path: "monitor",
          name: "监控中心",
          redirect: "/monitor/realtime",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "realtime",
              name: "实时监控",
              component: () => import("@/views/Monitor/MonitorRealtime.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "history",
              name: "历史记录",
              component: () => import("@/views/Monitor/MonitorHistory.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true  },
            },
            {
              path: "history/:time",
              name: "历史记录video",
              component: () => import("@/views/Monitor/MonitorHistory.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true  },
            },
          ],
        },
        {
          path: "farm",
          name: "农场",
          redirect: "/farm/fields",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "fields",
              name: "农田管理",
              component: () => import("@/views/Farm/FarmFields.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "data",
              name: "农场数据",
              component: () => import("@/views/Farm/FarmData.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },
        {
          path: "farmingPlan",
          name: "农事手册",
          redirect: "/farming/plan",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "plan",
              name: "种植计划",
              component: () => import("@/views/FarmingPlan/Plan.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "calendar",
              name: "日历",
              component: () => import("@/views/FarmingPlan/Calendar.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "recording",
              name: "农事记录",
              component: () => import("@/views/FarmingPlan/Recording.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "yield",
              name: "产量预测",
              component: () => import("@/views/FarmingPlan/Yield.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },
        {
          path: "traceability",
          name: "产品溯源",
          redirect: "/traceability/reports",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "reports",
              name: "溯源报表",
              component: () => import("@/views/Traceability/TraceabilityReports.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "abnormal",
              name: "异常溯源",
              component: () => import("@/views/Traceability/TraceabilityAbnormal.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "products",
              name: "溯源产品",
              component: () => import("@/views/Traceability/TraceabilityProducts.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "settings",
              name: "溯源设置",
              component: () => import("@/views/Traceability/TraceabilitySettings.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },
        {
          path: "PlantDiseasesAndPests",
          name: "病虫害检测",
          redirect: "/PlantDiseasesAndPests/UpdateImg",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "UpdateImg",
              name: "病虫害",
              component: () => import("@/views/PlantDiseasesAndPests/UpdateImg.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
          ],
        },
        {
          path: "system",
          name: "系统管理",
          redirect: "/system/users",
          meta: { keepAlive: true, isAuth: true, needLogin: true },
          children: [
            {
              path: "users",
              name: "用户管理",
              component: () => import("@/views/System/SystemUsers.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "roles",
              name: "角色权限",
              component: () => import("@/views/System/SystemRoles.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true },
            },
            {
              path: "personalCenter",
              name: "个人中心",
              component: () => import("@/views/System/PersonalCenter.vue"),
              meta: { keepAlive: true, isAuth: true, needLogin: true }
            }
          ],
        },
      ],
    },
    {
      path: '/Login',
      name: "登录页",
      component: () => import("../components/Login/Login.vue"),
    },
    {
      path: '/pictureRec',
      name: "图片识别",
      component: () => import("@/views/PictureRec/PictureRec.vue"),
    },
    {
      path: '/expert',
      component: Layout,
      redirect: '/expert/consultation',
      name: '农业专家系统',
      meta: {
        title: '农业专家系统',
        icon: 'ChatDotRound'
      },
      children: [
        {
          path: 'consultation',
          name: '智能咨询',
          component: () => import('@/views/Expert/Consultation.vue'),
          meta: { title: '智能咨询' }
        },
        {
          path: 'analysis',
          name: '数据分析',
          component: () => import('@/views/Expert/Analysis.vue'),
          meta: { title: '数据分析' }
        },
        {
          path: 'recommendation',
          name: '种植建议',
          component: () => import('@/views/Expert/Recommendation.vue'),
          meta: { title: '种植建议' }
        }
      ]
    },
  ],
});

// // 路由守卫
// router.beforeEach((to, from, next) => {
//   // 设置页面标题
//   document.title = to.meta.title as string || '智慧农业管理系统'

//   // 检查是否需要登录
//   if (to.meta.needLogin) {
//     if (localStorage.getItem("token")) {
//       next()
//     } else {
//       next({ name: "登录页", query: { redirect: to.fullPath } })
//     }
//   } else {
//     next()
//   }
// })

export default router;
